﻿//-----------------------------------------------------------------------
// <copyright file="Startup.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: Startup.cs
// * history : created by qinchaoyue 2017-11-30 01:39:56
// </copyright>
//-----------------------------------------------------------------------

namespace Slwy.Mvc
{
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Pivotal.Discovery.Client;

    using Slwy.FrameCore.Swagger;
    using Slwy.Mvc.Logging;
    using Slwy.Swagger;

    /// <summary>
    /// mvc 服务注册器
    /// 集中注册mvc需要的服务
    /// </summary>
    public static class Startup
    {
        /// <summary>
        /// 批量注册服务 
        /// </summary>
        /// <param name="services"> 服务集合 . </param>
        /// <param name="configuration"> The configuration.  </param>
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration, IHostingEnvironment currentEnvironment)
        {
            var env = currentEnvironment.EnvironmentName.ToUpper();

            if (env != "PRODUCTION")
            {
                // 注册 文档服务
                services.AddSwaggerService();
            }

            if (env != "DEVELOPMENT")
            {
                // 注册 Spring Cloud 服务发现
                services.AddDiscoveryClient(configuration);
            }

            services.AddMvcService();
        }

        /// <summary>
        /// mvc服务配置
        /// </summary>
        /// <param name="app">the app. </param>
        /// <param name="env">the env . </param>
        public static void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            var environmentName = env.EnvironmentName.ToUpper();

            if (environmentName != "PRODUCTION")
            {
                // 配置 Swagger 行为
                app.AddSwaggerConfigure();
            }
            if (environmentName != "DEVELOPMENT")
            {
                // 配置服务发现
                app.UseDiscoveryClient();
            }

            // 配置 日志记录服务
            app.UseRequestResponseLogging();

            // 配置mvc 
            app.UseMvc();

            // https 支持
            // var options = new RewriteOptions().AddRedirectToHttps(StatusCodes.Status301MovedPermanently, 44384);
            // app.UseRewriter(options);
        }
    }
}
